package org.example.myjavaweb_de.servlet;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.example.myjavaweb_de.User.Student;
import org.example.myjavaweb_de.padao.paqdao;
import org.example.myjavaweb_de.util.DbUtil;

import java.io.IOException;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static java.awt.SystemColor.text;

@WebServlet(urlPatterns = {"/myservelt"})
public class myservelt extends GenericServlet {

private int n;
    public myservelt(){
    System.out.println("<h1>myservelt<h1>");
        System.out.println("你好");
}

@Override
public void init(ServletConfig config) throws ServletException {
    System.out.println("myservelt");
}
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {

        String ss="<h1>hello"+Math.random()+"</h1>";
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        //请求设置中文
        request.setCharacterEncoding("utf-8");
        //响应设置中文编码
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().println("<h1>myservelt<h1>");
        response.getWriter().println(ss);
       /** paqdao xdao=new paqdao();
        List<Student> sa=xdao.queryall();
        response.getWriter().println("<table border=\"1\" cellspacing=\"0\" cellpadding=\"6\";>");
        for(Student s:sa){
            response.getWriter().println("<tr>");
            response.getWriter().println("<td>"+s.getStu()+"</td>");
            response.getWriter().println("<td>"+s.getStus()+"</td>");
            response.getWriter().println("</tr>");
        }
        response.getWriter().println("</table>");
        **/
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String select=request.getParameter("select");
        System.out.println(username+" "+password+" "+select);
        response.getWriter().println(username+" "+password+" "+select);
        //如果符合下列值，实现跳转
        if(username.equals("小米")&&password.equals("123456") ){
            //使用请求转发(浏览器url不变)
            //request.getRequestDispatcher("mmm").forward(request, response);
            response.sendRedirect("mm");
        }else {
            //使用重定向(url会变)
            response.sendRedirect("index.html");
        }
        //自动刷新页面
        //response.addIntHeader("refresh",3);
        //response.setHeader("refresh","3");
        //自动刷新跳转
        //response.setHeader("refresh","3;url=moudel.html");
        //复选框用数组接收，如果都没选，就是NUll,循环要进行判断,返回default值(value)为on

        String []checks=request.getParameterValues("check");
if(checks!=null){
    for(int i=0;i<checks.length;i++){
        System.out.println(checks[i]);
    }
}

        /**
        servletResponse.getWriter().println("<h1>myservelt<h1>");
        servletResponse.getWriter().println(ss);
        paqdao xdao=new paqdao();
        List<Student> sa=xdao.queryall();
        servletResponse.getWriter().println("<table border=\"1\" cellspacing=\"0\" cellpadding=\"6\";>");
        for(Student s:sa){
            servletResponse.getWriter().println("<tr>");
            servletResponse.getWriter().println("<td>"+s.getStu()+"</td>");
            servletResponse.getWriter().println("<td>"+s.getStus()+"</td>");
            servletResponse.getWriter().println("</tr>");
        }
        servletResponse.getWriter().println("</table>");
         **/
        n++;
        System.out.println(n);
    }

    @Override
    public void destroy(){
        System.out.println("myservelt");
    }
}
